package com.newbee.nsq;


import com.github.brainlag.nsq.NSQConfig;
import com.github.brainlag.nsq.NSQProducer;
import com.github.brainlag.nsq.exceptions.NSQException;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;

/**
 * Created by iss on 17/7/19.
 */
public class Product {
    public static void main(String[] args) throws NSQException,InterruptedException {

        NSQProducer producer = new NSQProducer();
        GenericKeyedObjectPoolConfig genericKeyedObjectPoolConfig = new GenericKeyedObjectPoolConfig();
        genericKeyedObjectPoolConfig.setMaxTotal(20);
        genericKeyedObjectPoolConfig.setMaxIdlePerKey(10);
        genericKeyedObjectPoolConfig.setMinIdlePerKey(10);
        genericKeyedObjectPoolConfig.setTestOnCreate(true);
        producer.setPoolConfig(genericKeyedObjectPoolConfig);
        producer.setExecutor(  Executors.newFixedThreadPool(10));
        producer.addAddress("127.0.0.1", 4150).start();
        for (int i =1; i <= 2; i++) {
            try {
                producer.produce("test", ("测试消息"+i).getBytes());
            } catch (TimeoutException e) {
                e.printStackTrace();
            }
            System.out.println("发送第" + i + "次");
           // Thread.sleep(10000);
        }
    }
}
